package com.cloudptt.api.product.func.call.group.fsm.video;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.pjsua2.MyCall;
import com.cloudptt.api.product.func.addr.IAddrOp;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallTimer;
import com.cloudptt.api.product.func.call.group.GroupCallVideoListener;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.call.GroupCallAgent;
import com.cloudptt.api.product.func.call.group.fsm.floor.ISubState;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.CallOperator;
import com.cloudptt.api.product.res.Resource;
import com.mcptt.db.model.AddrInfo;

import android.content.Context;

public class VideoAgent implements ISubState,IRollback
{
	private ISubState currentState = null;
	private GroupCallVideoListener listener = null;
	private Context ctx = null;
	private CallOperator operator = null;
	private IAddrOp addOp = null;
	private String callIdStr = MyCall.DEF_CALL_ID_STR;
	private IRollback rollback = null;
	private CallTimer callTimer = null;
	
	private AddrInfo videoUserInfo = null;

	public VideoAgent(Context ctx,String callIdStr,CallOperator operator,IAddrOp addOp,CallTimer callTimer,IRollback rollback)
	{
		Log.d(GroupCallAgent.Log_Key, "VideoAgent");
		this.ctx = ctx;
		this.callIdStr = callIdStr;
		this.operator = operator;
		this.addOp = addOp;
		this.callTimer = callTimer;
		this.rollback = rollback;
		this.currentState = new VideoNoPermissionState(this);
	}
	
	public void setListener(GroupCallVideoListener listener)
	{
		this.listener = listener;
	}
	
	public void goToState(int nextState){
		ISubState videoState = null;
		Log.d(GroupCallAgent.Log_Key, "goToState:["+ CallConst.stateTag(this.currentState.getState()) +
				" to "+ CallConst.stateTag(nextState) + "]");

			switch (nextState) {
			case CallConst.VideoState_NoPermission:
				videoState = new VideoNoPermissionState(this);
				break;
			case CallConst.VideoState_PendingRequest:
				videoState = new VideoPendingRequestState(ctx,this);
				break;
			case CallConst.VideoState_Permission:
				videoState = new VideoPermissionState(this);
				break;
			case CallConst.VideoState_PendingRelease:
				videoState = new VideoPendingReleaseState(ctx,this);
				break;
			default:
				Log.e(GroupCallAgent.Log_Key, "Illegal state="+ nextState);
				return;//!!!
			}
			this.setCurrentState(videoState);
	}

	private void setCurrentState(ISubState currentState) 
	{
		this.currentState.destory();
		this.currentState = currentState;
		
		if(listener != null )
		{
			listener.updateVideoState(currentState.getState());
		}
	}
	public void updateVideoPermissionNum(String num) 
	{
		if(listener != null
				&& num != null)
		{
			if(num.equals(""))
			{
				listener.updateVideoPermissionInfo(null);
				videoUserInfo = null;
			}
			else
			{
				videoUserInfo = addOp.getUserInfoByNum(num);
				listener.updateVideoPermissionInfo(videoUserInfo);
			}
		}
	}
//	public void notifyCurrentState() 
//	{
////		this.currentState.destory();
////		this.currentState = currentState;
//		int state = currentState.getState();
//		if(listener != null)
//		{
//			String name = this.currentState.getSpeakName();
//			listener.updateVideoState(state, name);
//		}
//	}

	public int doUserAction(int btnAction)
	{
		Log.d(GroupCallAgent.Log_Key, "doBtnAction is " + btnAction);
		return this.currentState.doUserAction(btnAction);
	}
	
	public void updateTime(CallTimer timer) {
		this.callTimer.updateTime(timer);
	}
	

	public void receiveRtcpMsg(RtcpMsg msg)
	{
		Log.d(GroupCallAgent.Log_Key, "receiveRtcpMsg msg = " + msg);
		this.currentState.receiveRtcpMsg(msg);
	}
	public void updateTakenUser(String callType,AddrInfo gInfo,AddrInfo user)
	{}
	public int getState()
	{
		return this.currentState.getState();
	}
	public void rollback()
	{
		Log.d(GroupCallAgent.Log_Key, "rollback");
		rollback.rollback();
	}

	@Override
	public void destory() {
		// TODO Auto-generated method stub
		this.currentState.destory();
	}
	
	
	protected int TransRequest()
	{
		Log.d(GroupCallAgent.Log_Key, "requestTrans");
		Context ctx = this.getCtx();
		if(null == operator
				|| null == ctx)
		{
			return CauseCont.OperateErr_SIP;
		}
		int result = operator.requestTrans(callIdStr);
		if( CauseCont.OperateCheck_Suc != result ) {
			result = CauseCont.OperateErr_SIP;
		}
		return result;
	}
	
	protected int transRelease()
	{
		Log.d(GroupCallAgent.Log_Key, "releaseTrans");
		Context ctx = this.getCtx();
		if(null == operator
				|| null == ctx)
		{
			return CauseCont.OperateErr_SIP;
		}
		int result = operator.releaseTrans(callIdStr);
		if( CauseCont.OperateCheck_Suc != result ) {
			result = CauseCont.OperateErr_SIP;
		}
		return result;
	}
	
	protected void opAudioMedia(int opType)
	{
		Log.d(GroupCallAgent.Log_Key, "opAudioMedia opType = " + opType);
		Context ctx = this.getCtx();
		if(null == operator
				|| null == ctx)
		{
			String errStr =  Resource.getString("operator_null");//ctx.getString(R.string.operator_null);
			CommDlg.showToast(ctx,errStr);
			return;
		}
		
		int result = operator.opAudioMedia(callIdStr,opType);
		if(result != CauseCont.OperateCheck_Suc) 
		{
			String errStr = CauseCont.getOperateCheckErrStr(result,ctx);
			CommDlg.showToast(ctx,errStr);
			return;
		}
	}
	
	protected void opVideoMedia(int opType)
	{
		Log.d(GroupCallAgent.Log_Key, "opVideoMedia opType = " + opType);
		Context ctx = this.getCtx();
		if(null == operator
				|| null == ctx)
		{
			String errStr =  Resource.getString("operator_null");//ctx.getString(R.string.operator_null);
			CommDlg.showToast(ctx,errStr);
			return;
		}
		
		int result = operator.opVideoMedia(callIdStr,opType);
		if(result != CauseCont.OperateCheck_Suc) 
		{
			String errStr = CauseCont.getOperateCheckErrStr(result,ctx);
			CommDlg.showToast(ctx,errStr);
			return;
		}
	}
	
	public AddrInfo getVideoUserInfo() {
		return videoUserInfo;
	}
	
	public Context getCtx() {
		return ctx;
	}

	public CallOperator getOperator() {
		return operator;
	}
	
	public CallTimer getCallTimer() {
		return callTimer;
	}
	
	public void clearResource()
	{
		currentState.destory();
		currentState = null;
		listener = null;
		ctx = null;
		operator = null;
	}
}
